package org.zzh.controller.system.dept;

import org.slf4j.LoggerFactory;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.zzh.controller.BaseController;
import org.zzh.dao.common.PageBean;
import org.zzh.domain.system.dept.PeDept;
import org.zzh.service.common.CommonService;
import org.zzh.service.system.dept.PeDeptService;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/dept")
public class PeDeptController extends BaseController {

    @Autowired
    PeDeptService peDeptService;

    @Autowired
    CommonService commonService;

    @RequestMapping(value = "/list.do")
    public ModelAndView list(
            @RequestParam(required = false, value = "page", defaultValue = "1") Integer page,
            @RequestParam(required = false, value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(required = false, value = "name", defaultValue = "") String name
    ) {
        PageBean<PeDept> list = peDeptService.list(page, pageSize, name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/WEB-INF/pages/system/dept/dept-list");
        modelAndView.addObject("pageBean", list);
        modelAndView.addObject("name", name);
        return modelAndView;
    }


    @RequestMapping(value = "/toAdd.do")
    public ModelAndView toAdd() {
        ModelAndView modelAndView = new ModelAndView();
        List<PeDept> deptList = peDeptService.findParentList();
        modelAndView.addObject("deptList", deptList);
        modelAndView.setViewName("/WEB-INF/pages/system/dept/dept-add");
        return modelAndView;
    }

    @RequestMapping(value = "/toUpdate.do")
    public ModelAndView toUpdate(String id) {
        ModelAndView modelAndView = new ModelAndView();
        PeDept dept = peDeptService.findById(id);
        List<PeDept> deptList = peDeptService.findParentList();
        modelAndView.addObject("deptList", deptList);
        modelAndView.addObject("dept", dept);
        modelAndView.setViewName("/WEB-INF/pages/system/dept/dept-update");
        return modelAndView;
    }


    @RequestMapping(value = "/edit.do")
    public String edit(PeDept dept) {
        if (dept.getDeptId() != null && !dept.getDeptId().isEmpty()) {
            peDeptService.update(dept);
            return "redirect:/system/dept/list.do";
        }
        dept.setDeptId(UUID.randomUUID().toString());
        PeDept peDept = (PeDept) session.getAttribute("company");
        if (peDept != null) {
            dept.setCompanyId(peDept.getCompanyId());
            dept.setCompanyName(peDept.getCompanyName());
        } else {
            dept.setCompanyId(companyId);
            dept.setCompanyName(companyName);
        }
        peDeptService.add(dept);
        return "redirect:/system/dept/list.do";
    }

    @RequestMapping(value = "/delete.do")
    public String delete(String[] ids) {
        peDeptService.deleteList(ids);
        return "redirect:/system/dept/list.do";
    }

}
